// for knockout.js

var PageData = function() {
  return {
    Request:{},
    SubHeader:'',
    Issues:[],
    Activities:[],
    HasMore:false
  };
}

var viewModel = {
  userData: ko.observable(),
  isLoggedIn: ko.observable(false),
  adminLinks: ko.observableArray(),
  setUserData: function(data) {
    this.userData(data);
    if (!data)
      return;
    this.adminLinks(data.AdminLinks);
    if (data.Email && data.Email.length > 0) {
      this.isLoggedIn(true);
      this.displayUser = data.Email;
    }
    if (data.IsAdmin) {
      $('.adminonly').removeClass('hide').show();
    }
  },

  contentsSubHeader: ko.observable(),

  pageData: ko.observable(new PageData()),
  setPageData: function(data) {
    $('.content-pagedata-loader').hide();
    $('.tmpl-generated-contents').empty();
    $('.content-pagedata').removeClass('hide').show();
    if (!data) {
      this.pageData(new PageData());
      return;
    }
    if (data && data.Request) {
      // Make sure we match with what is returned.
      this.selectUser(data.Request.User);
      this.selectView(data.Request.View);
    }
    if (data && data.UserSummary) {
      yolo.decorateWithHeatColor(data.UserSummary.RelatedUsers);
      yolo.decorateWithHeatColor(data.UserSummary.FileCommits);
    }
    this.pageData(data);
  },

  views: ['Home', 'Issues', 'Activities'],
  selectedView: ko.observable('Home'),
  selectView: function(view) {
    this.selectedView(view);
  },

  displayUser: '',
  selectedUser: ko.observable(),
  selectUser: function(user) {
    if (user && user.length > 0) {
      this.displayUser = user;
    }
    this.selectedUser(user);
  },

  selectViewAndUser: function(view, user) {
    this.selectUser(user);
    this.selectView(view);
    this.selectedIssue(null);
    this.loadNewPageData();
  },

  selectedPage: ko.observable('1'),
  selectPage: function(page) {
    this.selectedPage('' + page);
    this.loadNewPageData();
  },

  selectedIssue: ko.observable(),
  selectIssue: function(issue) {
    this.selectedIssue(issue);
    this.loadNewPageData();
  }
};

// It's intentional that this is not a dependentObservable.
viewModel.loadNewPageData = function() {
  if (this.userData() == null)
    return
  var data = { view: this.selectedView(),
               user: this.selectedUser(),
               page: this.selectedPage(),
               xsrf: yoloXsrfToken };
  console.log('requesting with', data);
  if (this.lastViewRequest)
    this.lastViewRequest.abort();  // prevent concurrent requests.
  $('.content-pagedata-loader').removeClass('hide').show()
  $('.content-pagedata').hide()
  this.contentsSubHeader(null);
  this.lastViewRequest = $.ajax({
    type: "POST",
    url: '/ajax/get_data',
    data: data,
    success: (function(data) {
      console.log('Got page data: ', data.Data);
      this.lastViewRequet = null;
      yoloXsrfToken = data.XSRFToken;
      this.setPageData(data.Data);
    }).bind(this),
    error: yolo.showAjaxError
  });
};

viewModel.toggleSelectIssue = function(issue) {
  if (issue == this.selectedIssue()) {
    this.selectedIssue(null);
  } else {
    this.selectedIssue(issue);
  }
};

$.ajax({
  type: "POST",
  url: '/ajax/get_user_data',
  data: { url:location.href, xsrf:yoloXsrfToken },
  dataType: 'json',
  success: (function(data) {
    console.log('Got page settings: ', data.Data);
    yoloXsrfToken = data.XSRFToken;
    this.setUserData(data.Data);
    this.loadNewPageData();
  }).bind(viewModel),
  error: yolo.showAjaxError
});

window.yoloViewModel = viewModel;
ko.applyBindings(viewModel);

ko.linkObservableToUrl(viewModel.selectedView, "view", "Home");
ko.linkObservableToUrl(viewModel.selectedUser, "user");
ko.linkObservableToUrl(viewModel.selectedPage, "page", "1");
ko.linkObservableToUrl(viewModel.selectedIssue, "issue");
